////////////////////////////////////////////////////////////////////////////////
// Copyright 2006 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   COMPANY
// AUTHOR           :   Victor Zheng 
// DATE CREATE      :   2006/02/01
// DESCRIPTION      :   a DAO file for the function, COMPANY
//                  :   
//                  :   
//                  :   
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2006/02/01   Victor Zheng    1.0                 source is generated.
//
//
//
////////////////////////////////////////////////////////////////////////////////

package com.erp.app.admin.companySetup.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.erp.app.admin.companySetup.criteria.CompanySrchCriteria;
import com.erp.app.admin.companySetup.pk.CompanyPk;
import com.erp.app.admin.companySetup.vo.CompanyVo;
import com.erp.app.common.ProjectConstants;
import com.erp.framework.exceptions.FindRecordException;
import com.erp.framework.model.criteria.SimpleBaseSrchCriteria;
import com.erp.framework.model.dao.SimpleSearchMaintStyleDao;
import com.erp.framework.model.pk.SimpleBasePk;
import com.erp.framework.model.vo.ChildList;
import com.erp.framework.model.vo.SimpleBaseVo;
import com.erp.framework.types.CustomBigInteger;

/**
* <p>A <strong>DAO</strong>(Data Access Object) abstracts and encapsulates
* all access to the persistent store.  The DAO manages the connection with the
* data source to obtain and store data.  Also, the DAO implements
* the access mechanism required to work with the data source,
* and always provides a uniform API to its clients.
*
* @author          Victor Zheng 
* @version 1.0     2006/02/01
* @since           FrameWork 1.0
*
*/
public class CompanyDao extends SimpleSearchMaintStyleDao {

	/**
	* Returns the SQL select statement.
	* @return        a string containing sql query
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected String sqlSelectByPrimaryKey(){

		return sqlFind();
	}

	/**
	* Sets the values of a result set to a pk
	* @param         pStmt   the incoming prepared statement
	* @param         pk      the searching primary key
	* @return        a vo containing the result set values
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected void setValuesForSelectByPrimaryKey(PreparedStatement pStmt, SimpleBasePk pk) throws SQLException{

		setValuesForFind(pStmt, pk);
	}

	/**
	* Returns the constructed model from a result set.
	* @param         rs     the incoming result set
	* @return        a constructed VO from the result set
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected SimpleBaseVo getModelForSelectByPrimarKey(ResultSet rs) throws SQLException {

		return getResultSetValues(rs);
	}


	/**
	* Returns the SQL select statement.
	* @param         criteria     the searching criteria
	* @return        a string containing sql query
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected String sqlSelectByCriteria(SimpleBaseSrchCriteria criteria) {

		return sqlQuery(criteria);
	}

	/**
	* Returns the constructed model from a result set.
	* @param         rs     the incoming result set
	* @return        a constructed VO from the result set
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected SimpleBaseVo getModelForSelectByCriteria(ResultSet rs) throws SQLException {

		return getResultSetValues(rs);
	}

	/**
	* Returns the constructed primary key from a result set.
	* @param         rs     the incoming result set
	* @return        a constructed PK from the result set
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected SimpleBasePk getPkForSelectByCriteria(ResultSet rs) throws SQLException {

//		ErrorMsgLabelPk pk = new ErrorMsgLabelPk(rs.getString("COMPANY_CODE"),
//			rs.getString("GROUP_ID"), rs.getString("MENU_ID"), rs.getString("OBJECT_ID"),
//			rs.getString("LANG_CODE"));
//		return pk;

		return null;
	}


	/**
	* Returns the SQL select statement.
	* @param         obj     the searching criteria
	* @return        a string containing sql query
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected String sqlQuery(SimpleBaseSrchCriteria obj) {

		CompanySrchCriteria criteria = (CompanySrchCriteria) obj;
		String sql = "";
		String whereClause = toWhereClause(criteria);
		String groupByClause = "";
		String orderByClause = "ORDER BY COMPANY_CODE";

		if(!criteria.isSearchResult()){
			sql = "SELECT COMPANY_CODE, SHORT_NAME, LONG_NAME, ADDR1, ADDR2, "
			+ "ADDR3, COUNTRY, PHONE, FAX, CCY, "
			+ "BASE_CCY, CONTENT_TYPE, COMPANY_LOGO, CREATE_USER, CREATE_DATE, "
			+ "UPDATE_USER, UPDATE_DATE, ADDR4, LEDGER_CODE, POSTAL_CODE, "
			+ "PRIVACY_CODE, PRINT_FORM_FONT, PHONE_AREA, PHONE_COUNTRY, FAX_AREA, FAX_COUNTRY "
			+ "FROM T_COMPANY "
			+ whereClause
			+ groupByClause
			+ orderByClause;
		} else {
			sql = "SELECT COMPANY_CODE, SHORT_NAME, LONG_NAME, ADDR1, ADDR2, "
				+ "ADDR3, COUNTRY, PHONE, FAX, CCY, "
				+ "BASE_CCY, CONTENT_TYPE, COMPANY_LOGO, CREATE_USER, CREATE_DATE, "
				+ "UPDATE_USER, UPDATE_DATE, ADDR4, LEDGER_CODE, POSTAL_CODE, "
				+ "PRIVACY_CODE, PRINT_FORM_FONT, PHONE_AREA, PHONE_COUNTRY, FAX_AREA, FAX_COUNTRY "
				+ "FROM T_COMPANY "
				+ whereClause
				+ groupByClause
				+ orderByClause;
		}
		//super.logSqlQuery(sql);
		return sql;
	}

	/**
	* Returns the where clause of a SQL select statement.
	* @param         obj     the searching criteria
	* @return        a string containing the where clause
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected String toWhereClause(CompanySrchCriteria crit) {

		CompanySrchCriteria criteria = (CompanySrchCriteria) crit;
		StringBuffer whereClause = new StringBuffer();

		if (criteria.getSrchCompanyCode() != null) {
			whereClause.append(whereClause.length()== 0? "WHERE ": "AND ");
			whereClause.append(" COMPANY_CODE = '" + criteria.getSrchCompanyCode().trim().toUpperCase()+"' ");
		}
		
		if (criteria.getSrchShortNameFr() != null) {
			whereClause.append(whereClause.length() == 0 ? "WHERE " : "AND ");
			whereClause.append("SHORT_NAME >= '"
				+ criteria.getSrchShortNameFr().toUpperCase().trim() + "' ");
		}
		if (criteria.getSrchShortNameTo() != null) {
			whereClause.append(whereClause.length() == 0 ? "WHERE " : "AND ");
			whereClause.append("SHORT_NAME <= '"
				+ criteria.getSrchShortNameTo().toUpperCase().trim() + "' ");
		}
		
		if (criteria.getSrchLongNameFr() != null) {
			whereClause.append(whereClause.length() == 0 ? "WHERE " : "AND ");
			whereClause.append("LONG_NAME >= '"
				+ criteria.getSrchLongNameFr().toUpperCase().trim() + "' ");
		}
		if (criteria.getSrchLongNameTo() != null) {
			whereClause.append(whereClause.length() == 0 ? "WHERE " : "AND ");
			whereClause.append("LONG_NAME <= '"
				+ criteria.getSrchLongNameTo().toUpperCase().trim() + "' ");
		}		
		
		return whereClause.toString();
	}

	/**
	* Returns an array list of child VOs by the parent
	* @param         parent   the incoming parent VO
	* @param         conn     the database connection
	* @return        an array list of child VOs
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	public ChildList selectByParent(SimpleBaseVo parent, Connection conn)
		throws FindRecordException {

		return null;
	}

	/**
	* Constructs a string of SQL insert statement
	* @return        a string of SQL insert statement
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected String sqlInsert() {

		String sql = "INSERT INTO T_COMPANY ("
			+ "COMPANY_CODE, SHORT_NAME, LONG_NAME, ADDR1, ADDR2, "
			+ "ADDR3, COUNTRY, PHONE, FAX, CCY, "
			+ "BASE_CCY, CONTENT_TYPE, COMPANY_LOGO, CREATE_USER, CREATE_DATE, "
			+ "UPDATE_USER, UPDATE_DATE, ADDR4, LEDGER_CODE, POSTAL_CODE, "
			+ "PRIVACY_CODE, PRINT_FORM_FONT, PHONE_AREA, PHONE_COUNTRY, FAX_AREA, FAX_COUNTRY) "
			+ "VALUES ("
			+ "?, ?, ?, ?, ?, "
			+ "?, ?, ?, ?, ?, "
			+ "?, ?, ?, ?, SYSDATE, "
			+ "?, ?, ?, ?, ?, "
			+ "?, ?, ?, ?, ?, ?)";

		//super.logSqlQuery(sql);
		return sql;
	}

	/**
	* Sets the values for a SQL insert or update statement.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming VO
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected void setValuesForInsertAndModify(PreparedStatement pStmt, Object obj, String opType)
		throws SQLException {

		CompanyVo vo = (CompanyVo) obj;
		int i = 1;

        if (ProjectConstants.DB_OP_INSERT.equals(opType)){
            vo.setCompanyCode(vo.getCompanyCode().toUpperCase());
        }

		pStmt.setString(i++, vo.getCompanyCode());
		setNullableString(pStmt, i++, vo.getShortName());
		setNullableString(pStmt, i++, vo.getLongName());
		setNullableString(pStmt, i++, vo.getAddr1());
		setNullableString(pStmt, i++, vo.getAddr2());
		setNullableString(pStmt, i++, vo.getAddr3());
		setNullableString(pStmt, i++, vo.getCountry());
		setNullableString(pStmt, i++, vo.getPhone());
		setNullableString(pStmt, i++, vo.getFax());
		setNullableString(pStmt, i++, vo.getCcy());
		setNullableString(pStmt, i++, vo.getBaseCcy());
		setNullableString(pStmt, i++, vo.getContentType());
		setNullableBytes(pStmt, i++, vo.getCompanyLogo());
		setNullableString(pStmt, i++, vo.getCreateUser());

		if (ProjectConstants.DB_OP_UPDATE.equals(opType))
			setNullableTimestamp(pStmt, i++, vo.getCreateDate());

		setNullableString(pStmt, i++, vo.getUpdateUser());

		if (ProjectConstants.DB_OP_INSERT.equals(opType))
			setNullableTimestamp(pStmt, i++, vo.getUpdateDate());
		setNullableString(pStmt, i++, vo.getAddr4());
		setNullableString(pStmt, i++, vo.getLedgerCode());
		setNullableString(pStmt, i++, vo.getPostalCode());
		setNullableBigInteger(pStmt, i++, vo.getPrivacyCode());
		setNullableString(pStmt, i++, vo.getPrintFormFont());
		setNullableString(pStmt, i++, vo.getPhoneArea());
		setNullableString(pStmt, i++, vo.getPhoneCountry());
		setNullableString(pStmt, i++, vo.getFaxArea());
		setNullableString(pStmt, i++, vo.getFaxCountry());

		if(ProjectConstants.DB_OP_UPDATE.equals(opType)){
			pStmt.setString(i++, vo.getCompanyCode());
			setNullableTimestamp(pStmt, i++, vo.getUpdateDate());
		}
	}

	/**
	* Sets the values for a SQL insert statement.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming VO
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected void setValuesForInsert(PreparedStatement pStmt, Object obj)
		throws SQLException {

		setValuesForInsertAndModify(pStmt, obj, ProjectConstants.DB_OP_INSERT);
	}

	/**
	* Constructs a string of SQL update statement
	* @return        a string of SQL update statement
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected String sqlUpdate() {

		String sql = "UPDATE T_COMPANY SET COMPANY_CODE=?, SHORT_NAME=?, LONG_NAME=?, ADDR1=?, ADDR2=?, "
			+ "ADDR3=?, COUNTRY=?, PHONE=?, FAX=?, CCY=?, "
			+ "BASE_CCY=?, CONTENT_TYPE=?, COMPANY_LOGO=?, CREATE_USER=?, CREATE_DATE=?, "
			+ "UPDATE_USER=?, UPDATE_DATE=SYSDATE, ADDR4=?, LEDGER_CODE=?, POSTAL_CODE=?, "
			+ "PRIVACY_CODE=?, PRINT_FORM_FONT=?, PHONE_AREA=?, PHONE_COUNTRY=?, FAX_AREA=?, FAX_COUNTRY=? "
			+ "WHERE COMPANY_CODE=? "
			+ "AND (UPDATE_DATE IS NULL OR UPDATE_DATE = ?) ";

		//super.logSqlQuery(sql);
		return sql;
	}

	/**
	* Sets the values for a SQL update statement.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming VO
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected void setValuesForModify(PreparedStatement pStmt, Object obj)
		throws SQLException {

		setValuesForInsertAndModify(pStmt, obj, ProjectConstants.DB_OP_UPDATE);
	}

	/**
	* Constructs a string of SQL delete statement
	* @return        a string of SQL update statement
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected String sqlDelete() {
		String sql = "DELETE FROM T_COMPANY WHERE COMPANY_CODE=? ";

		//super.logSqlQuery(sql);
		return sql;
	}

	/**
	* Sets the values for a SQL delete statement.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming VO
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected void setValuesForDelete(PreparedStatement pStmt, Object obj)
		throws SQLException {

		CompanyVo vo = (CompanyVo) obj;
		int i = 1;

		pStmt.setString(i++, vo.getCompanyCode());
	}

	/**
	* Constructs a string of SQL select statement
	* @return        a string of SQL select statement
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected String sqlFind() {

		String sql = "SELECT COMPANY_CODE, SHORT_NAME, LONG_NAME, ADDR1, ADDR2, "
			+ "ADDR3, COUNTRY, PHONE, FAX, CCY, "
			+ "BASE_CCY, CONTENT_TYPE, COMPANY_LOGO, CREATE_USER, CREATE_DATE, "
			+ "UPDATE_USER, UPDATE_DATE, ADDR4, LEDGER_CODE, POSTAL_CODE, "
			+ "PRIVACY_CODE, PRINT_FORM_FONT, PHONE_AREA, PHONE_COUNTRY, FAX_AREA, FAX_COUNTRY "
			+ "FROM T_COMPANY "
			+ "WHERE COMPANY_CODE=? "
			+ "ORDER BY COMPANY_CODE";

		//super.logSqlQuery(sql);
		return sql;
	}

	/**
	* Sets the values for a SQL select statement for finding by primary key.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming PK
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected void setValuesForFind(PreparedStatement pStmt, Object obj)
		throws SQLException {

		CompanyPk pk = (CompanyPk) obj;
		int i = 1;

		pStmt.setString(i++, pk.getCompanyCode());
	}

	/**
	* Constructs a string of SQL select statement
	* @return        a string of SQL select statement
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected String sqlList() {

		return null;
	}

	/**
	* Sets the values for a SQL select statement for finding child tables.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming parent PK
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected void setValuesForList(PreparedStatement pStmt, Object obj)
		throws SQLException {

	}

	/**
	* Sets the values of a result set to a vo for a search screen
	* @param         rs   the incoming result set
	* @return        a vo containing the result set values
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected SimpleBaseVo getSearchResultSetValues(ResultSet rs) throws SQLException {


		return getResultSetValues(rs);
	}

	/**
	* Sets the values of a result set to a vo
	* @param         rs   the incoming result set
	* @return        a vo containing the result set values
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected SimpleBaseVo getResultSetValues(ResultSet rs) throws SQLException {

		CompanyVo vo = new CompanyVo();

		vo.setCompanyCode(rs.getString("COMPANY_CODE"));
		vo.setShortName(rs.getString("SHORT_NAME"));
		vo.setLongName(rs.getString("LONG_NAME"));
		vo.setAddr1(rs.getString("ADDR1"));
		vo.setAddr2(rs.getString("ADDR2"));
		vo.setAddr3(rs.getString("ADDR3"));
		vo.setCountry(rs.getString("COUNTRY"));
		vo.setPhone(rs.getString("PHONE"));
		vo.setFax(rs.getString("FAX"));
		vo.setCcy(rs.getString("CCY"));
		vo.setBaseCcy(rs.getString("BASE_CCY"));
		vo.setContentType(rs.getString("CONTENT_TYPE"));
		vo.setCompanyLogo(rs.getBytes("COMPANY_LOGO"));
		vo.setCreateUser(rs.getString("CREATE_USER"));
		vo.setCreateDate(rs.getTimestamp("CREATE_DATE"));
		vo.setUpdateUser(rs.getString("UPDATE_USER"));
		vo.setUpdateDate(rs.getTimestamp("UPDATE_DATE"));
		vo.setAddr4(rs.getString("ADDR4"));
		vo.setLedgerCode(rs.getString("LEDGER_CODE"));
		vo.setPostalCode(rs.getString("POSTAL_CODE"));
		vo.setPrivacyCode(CustomBigInteger.customize(rs.getBigDecimal("PRIVACY_CODE")));
		vo.setPrintFormFont(rs.getString("PRINT_FORM_FONT"));
		vo.setPhoneArea(rs.getString("PHONE_AREA"));
		vo.setPhoneCountry(rs.getString("PHONE_COUNTRY"));
		vo.setFaxArea(rs.getString("FAX_AREA"));
		vo.setFaxCountry(rs.getString("FAX_COUNTRY"));

		return vo;
	}

}
